package com.jc.cloud.sign.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jc.cloud.sign.bean.dto.*;
import com.jc.cloud.sign.config.tencent.TencentAgent;
import com.jc.cloud.sign.service.TencentSignService;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.essbasic.v20210526.models.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TencentSingServiceImpl implements TencentSignService {
    @Resource
    TencentAgent tencentAgent;

    @Override
    public AbstractModel createConsoleLoginUrl(CeateConsoleLoginUrlDTO vo) {
        try {
            TencentBase tencentBase = tencentAgent.buildBaseAgent();

            // 实例化一个请求对象,每个接口都会对应一个request对象
            CreateConsoleLoginUrlRequest req = new CreateConsoleLoginUrlRequest();
            req.setAgent(tencentBase.getAgent());

            BeanUtil.copyProperties(vo,req,true);

            return tencentBase.getClient().CreateConsoleLoginUrl(req);
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new RuntimeException(exception);
        }
    }

    @Override
    public HashMap<Object, Object> getDescribeFlowTemplates(DescribeTemplatesDTO vo) {
        try {
            TencentBase tencentBase = tencentAgent.buildBaseAgent();
            DescribeTemplatesRequest req = new DescribeTemplatesRequest();
            req.setAgent(tencentBase.getAgent());

            BeanUtil.copyProperties(vo,req,true);

            DescribeTemplatesResponse describeTemplatesResponse = tencentBase.getClient().DescribeTemplates(req);
            TemplateInfo[] templates = describeTemplatesResponse.getTemplates();

            // 创建 List
            List<TemplateObject> templateList = new ArrayList<>();

            for (int i = 0; i <templates.length ; i++) {
                TemplateObject templateObject = new TemplateObject();
                    templateObject.setTemplateName(templates[i].getTemplateName());
                    templateObject.setTemplateId(templates[i].getTemplateId());
                // 在构造函数中设置 time 字段为当前时间
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                templateObject.setTime(dateFormat.format(new Date()));
                templateList.add(templateObject);
            }
            Long limit = describeTemplatesResponse.getLimit();
            Long offset = describeTemplatesResponse.getOffset();
            Long totalCount = describeTemplatesResponse.getTotalCount();
//           创建map
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("template",templateList);
            objectObjectHashMap.put("limit",limit);
            objectObjectHashMap.put("offset",offset);
            objectObjectHashMap.put("totalCount",totalCount);

            return objectObjectHashMap;
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }

    /**
    * Description: 嵌入web链接
    */
    @Override
    public AbstractModel getEmbedWebUrl(EmbedWebUrlDTO vo) {
        try {
            TencentBase tencentBase = tencentAgent.buildEmbedWebUrl();
            ChannelCreateEmbedWebUrlRequest req = new ChannelCreateEmbedWebUrlRequest();
            req.setAgent(tencentBase.getAgent());
//            req.setEmbedType(vo.getEmbedType());

            BeanUtil.copyProperties(vo,req,true);

            return tencentBase.getClient().ChannelCreateEmbedWebUrl(req);
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }


}
